import { useRequest } from "ahooks";
import {
  getTagListMapper,
  createTagMapper,
  updateTagMapper,
  getTagByIDMapper,
  getTagListByTypeMapper,
} from "../sql";
import { GetTagListDTO } from "../types";
import { toast } from "sonner";
import { TagTypeEnum } from "@prisma/client";

export const getTagList = (params: GetTagListDTO) => {
  return useRequest(() => getTagListMapper(params), {
    refreshDeps: [params],
  });
};

export const getTagListByType = (type: TagTypeEnum) => {
  return useRequest(() => getTagListByTypeMapper(type), {
    refreshDeps: [type],
  });
};

export const getTagById = (id: string) => {
  return useRequest(() => getTagByIDMapper(id), {
    refreshDeps: [id],
  });
};

export const createTag = () => {
  return useRequest(createTagMapper, {
    manual: true,
    onSuccess: () => {
      toast.success("创建成功");
    },
    onError(e) {
      toast.error(e.message);
    },
  });
};

export const updateTag = () => {
  return useRequest(updateTagMapper, {
    manual: true,
    onSuccess: () => {
      toast.success("更新成功");
    },
    onError(e) {
      toast.error(e.message);
    },
  });
};
